<?php
$model = new BDynamicFormModel();
$modelClass = get_class($model);
$screen = Blyn::app()->getUIManager()->getCurrentScreen();
$url = $screen->getAjaxLinkUrl('saveMessage', NULL, array(
    'messageId' => $brandId,
    'parentEventId' => $eventId,
    'postModel' => $modelClass));
$updateId = $screen->getAjaxContainerId();

$brandNameField = 'message_name_' . $brandId;
$imageField = 'image_media_image_' . $brandId;

$model->addField($brandNameField);
$model->addField($imageField);

if ($brandId > 0) {
    $brand = new BWMessage($brandId);

    $model->$brandNameField = $brand->name;
    $imageMedia = $brand->getMediaManager()->getMedia('image');
    if ($imageMedia)
        $model->$imageField = $brand->getMediaManager()->getMedia('image')->content;
}
?>
<?php
//$screen = Blyn::app()->getUIManager()->getCurrentScreen();
//$url = $screen->getAjaxLinkUrl('saveMessage', NULL, array('messageId' => $brandId,'parentEventId' => $eventId));
//$updateId = $screen->getAjaxContainerId();
?>
<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
    function send<?php echo $brandId;?>() {

//        var formData = new FormData($("#message-form")[0]);
var formElement = document.getElementById("brand-form");
        var formData = new FormData(formElement);
        
        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(data) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").load("<?php echo $screen->getLayoutView(); ?>");
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
            },
            error: function(data) {
                alert("There may a error on uploading. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }
</script>
<?php
//$model = new BWMessageFormModel();
//if ($brandId > 0) {//for modify
//    $brand = new BWMessage($brandId);
//    $model->name = $brand->name;
//    $model->image = $brand->getMediaManager()->getMedia('image')->content;
//}
?>

<div id="myModal<?php echo $brandId;?>" class="modal hide fade" style="width: 1000px;left: 33%; " tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3>品牌优惠</h3>
</div>
<div class="modal-body" id="PinPaiYouhui" style="display:block">

    <table class="table table-bordered table-striped">
        <colgroup>
            <col class="span1">
            <col class="span7">
        </colgroup>
        <thead>
            <tr>
                <th>品牌属性</th>
                <th>详细内容</th>
            </tr>
        </thead>
        <tbody>
            <tr><td> <code>品牌名称</code> </td>
<!--                <td>劲霸男装</td>-->
                <td><?php echo $form->textArea($model, $brandNameField, array('placeholder' => '品牌名称...', 'required' => true)); ?></td>
                
            </tr>

            <tr><td><code>品牌logo</code> </td>
                <td>
                    <?php echo $form->fileField($model, $imageField);
                    if($brandId > 0) {
                        $imageUrl = Yii::app()->request->baseUrl.$model->$imageField;
                        echo "<img src=".$imageUrl." width=50 height=50 />";
                    }
                    ?>
                </td>
            </tr>
        </tbody>
    </table>
</div> 
<div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>
    <button class="btn" data-dismiss="modal" aria-hidden="true" onclick="javascript:send<?php echo $brandId;?>()">保存</button>
  
</div>
</div>
